﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace MvcCms.Service.Validation
{
    public class IntLengthRangeAttribute : ValidationAttribute
    {
        public int Minimum { get; set; }
        public int Maximum { get; set; }

        public IntLengthRangeAttribute()
        {
            this.Minimum = 0;
            this.Maximum = 50;
        }

        public override bool IsValid(object value)
        {
            int? intValue = value as int?;
            var strValue = intValue.GetValueOrDefault().ToString();
            if (!string.IsNullOrEmpty(strValue))
            {
                int len = strValue.Length;
                return len >= this.Minimum && len <= this.Maximum;
            }
            return true;
        }
    }
}
